package GameMain.View.PTGame
{
	import GameMain.Game;
	import GameMain.Macro.Macro;
	import GameMain.Model.MapSource;
	import GameMain.View.PTGame.PlayerHP;
	
	import flash.display.BitmapData;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.utils.setTimeout;
	
	public class GameManager
	{
		
		
		public static var BooleanLoad:Boolean = false;
		
		private static var _attackEffectClass:Class = null;	//攻击效果
		private static var _addNumClass:Class = null;			//血量加
		private static var _subNumClass:Class = null;			//血量减
		private static var _content:Sprite = null;	
		private static var _playerHpBarClass:Class = null;	//血条
		private static var _gameBackClass:Class = null;		//游戏层部
		
		private static const MAX_LOAD_NUM:int = 25;
		
		
		private static const PATH_BODY:String = "config/Swf/Body/";
		private static const PATH_HEAD:String = "config/Swf/Head/";
		private static const PATH_WEAPON:String = "config/Swf/weapon/";
		private static const PATH_BOSS:String = "config/Swf/Boss/";
		private static const PATH_SIKLL:String = "config/Swf/Skill/";
		private static const PATH_MAP:String = "config/Map/";
		
		
		private static var Profession_Head_Class_List:Object = {};
		private static var Profession_Body_Class_List:Object = {};
		private static var Profession_Weapon_Class_list:Object = {};
		private static var NPC_Class_List:Object = {};
		private static var Map_Source_List:Object = {};
		private static var Sikll_Class_List:Object = {};
		
		private static var _backFun:Function = null;
		private static var _pvpGame:PVPGame = null;
		private static var _loadNumber:int = 0;
		
		
		public static function Init(info:LoaderInfo):void
		{
			_attackEffectClass = info.applicationDomain.getDefinition("Fla.PTGame.attackEffects") as Class;
			_addNumClass = info.applicationDomain.getDefinition("Fla.PTGame.HurtValueADD") as Class;
			_subNumClass = info.applicationDomain.getDefinition("Fla.PTGame.HurtValueSub") as Class;
			_playerHpBarClass = info.applicationDomain.getDefinition("Fla.PTGame.PlayerHPBar") as Class;
			_gameBackClass = info.applicationDomain.getDefinition("Fla.PTGame.DiBuQuan") as Class;
			_content = info.content as Sprite;
			PVP_Game.setLoseWinSprite(info.applicationDomain.getDefinition("Fla.PTGame.LoseWin") as Class)
		}
		
		
		public static function get ClassGameBack():Class
		{
			return _gameBackClass;
		}
		
		public static function get AddNumClass():Class
		{
			return _addNumClass;
		}
		
		public static function get SubNumClass():Class
		{
			return _subNumClass;
		}
		
		public static function get playerHpBarClass():Class
		{
			return _playerHpBarClass;
		}
		
		public static function getSikllClass(skillID:int):Class
		{
			return Sikll_Class_List[skillID];
		}
		
		public static function get PVP_Game():PVPGame
		{
			if(_pvpGame == null)
			{
				_pvpGame = new PVPGame(_content);
				Game.ModuleMgr.insertModule(Macro.MODULE_PVP_GAME,_pvpGame);
			}
			return _pvpGame;
		}
		
		public static function get ClassAttackEffect():Class
		{
			return _attackEffectClass
		}
		
		public static function setCallBackFun(fun:Function):void
		{
			_backFun = fun;
		}
		
		public static function getMapDataFromID(mapID:int):MapSource
		{
			return Map_Source_List[mapID];
		}
		
		public static function getHeadFromProfession(proID:int):Object
		{
			return new Profession_Head_Class_List[proID];
		}
		
		public static function getBodyFromProfession(proID:int):Object
		{
			return new Profession_Body_Class_List[proID];
		}
		
		public static function getWeaponFromProfession(proID:int):Object
		{
			return new Profession_Weapon_Class_list[proID];
		}
		
		public static function getNpcClass(npcID:int):Class
		{
			return NPC_Class_List[npcID];
		}
		
		public static function LoadProfessionModel():void
		{
			Game.EngineMgr.loadSWF(PATH_BODY+"body1.swf",onSwfLoadCompulete);
			Game.EngineMgr.loadSWF(PATH_BODY+"body2.swf",onSwfLoadCompulete);
			Game.EngineMgr.loadSWF(PATH_BODY+"body3.swf",onSwfLoadCompulete);
			Game.EngineMgr.loadSWF(PATH_BODY+"body4.swf",onSwfLoadCompulete);
			Game.EngineMgr.loadSWF(PATH_BODY+"body5.swf",onSwfLoadCompulete);
			Game.EngineMgr.startloading();
			_loadNumber = 0;
			sendPreToServer();
			function onSwfLoadCompulete(e:Event,path:String):void
			{
				_loadNumber++;
				var info:LoaderInfo = e.currentTarget as LoaderInfo;
				var _class:Class = info.applicationDomain.getDefinition("ViewMain") as Class;
				switch(path)
				{
					case PATH_BODY+"body1.swf":
						Profession_Body_Class_List[1] = _class;
						break;
					case PATH_BODY+"body2.swf":
						Profession_Body_Class_List[2] = _class;
						break;
					case PATH_BODY+"body3.swf":
						Profession_Body_Class_List[3] = _class;
						break;
					case PATH_BODY+"body4.swf":
						Profession_Body_Class_List[4] = _class;
						break;
					case PATH_BODY+"body5.swf":
						Profession_Body_Class_List[5] = _class;
						break;
				}
				
				if(_loadNumber >= 4.9)
					loadhead()
			}
		}
		
		private static function sendPreToServer():void
		{
			_backFun(_loadNumber/MAX_LOAD_NUM*100);
			if(_loadNumber < MAX_LOAD_NUM)
				setTimeout(sendPreToServer,1000);
			else
				BooleanLoad = true;
		}
		
		private static function loadhead():void
		{
			Game.EngineMgr.loadSWF(PATH_HEAD+"head1_5.swf",onSwfLoadCompulete);
			Game.EngineMgr.loadSWF(PATH_HEAD+"head2_5.swf",onSwfLoadCompulete);
			Game.EngineMgr.loadSWF(PATH_HEAD+"head3_5.swf",onSwfLoadCompulete);
			Game.EngineMgr.loadSWF(PATH_HEAD+"head4_5.swf",onSwfLoadCompulete);
			Game.EngineMgr.loadSWF(PATH_HEAD+"head5_5.swf",onSwfLoadCompulete);
			Game.EngineMgr.startloading();
			
			function onSwfLoadCompulete(e:Event,path:String):void
			{
				_loadNumber++;
				var info:LoaderInfo = e.currentTarget as LoaderInfo;
				var _class:Class = info.applicationDomain.getDefinition("ViewMain") as Class;
				switch(path)
				{
					case PATH_HEAD+"head1_5.swf":
						Profession_Head_Class_List[1] = _class;
						break;
					case PATH_HEAD+"head2_5.swf":
						Profession_Head_Class_List[2] = _class;
						break;
					case PATH_HEAD+"head3_5.swf":
						Profession_Head_Class_List[3] = _class;
						break;
					case PATH_HEAD+"head4_5.swf":
						Profession_Head_Class_List[4] = _class;
						break;
					case PATH_HEAD+"head5_5.swf":
						Profession_Head_Class_List[5] = _class;
						break;
				}
				if(_loadNumber >= 9.9)
					loadWeapon()
			}
		}
		
		private static function loadWeapon():void
		{
			Game.EngineMgr.loadSWF(PATH_WEAPON+"weapon1_2.swf",onSwfLoadCompulete);
			Game.EngineMgr.loadSWF(PATH_WEAPON+"weapon2_2.swf",onSwfLoadCompulete);
			Game.EngineMgr.loadSWF(PATH_WEAPON+"weapon3_2.swf",onSwfLoadCompulete);
			Game.EngineMgr.loadSWF(PATH_WEAPON+"weapon4_2.swf",onSwfLoadCompulete);
			Game.EngineMgr.loadSWF(PATH_WEAPON+"weapon5_2.swf",onSwfLoadCompulete);
			Game.EngineMgr.startloading();
			
			function onSwfLoadCompulete(e:Event,path:String):void
			{
				_loadNumber++;
				var info:LoaderInfo = e.currentTarget as LoaderInfo;
				var _class:Class = info.applicationDomain.getDefinition("ViewMain") as Class;
				switch(path)
				{
					case PATH_WEAPON+"weapon1_2.swf":
						Profession_Weapon_Class_list[1] = _class;
						break;
					case PATH_WEAPON+"weapon2_2.swf":
						Profession_Weapon_Class_list[2] = _class;
						break;
					case PATH_WEAPON+"weapon3_2.swf":
						Profession_Weapon_Class_list[3] = _class;
						break;
					case PATH_WEAPON+"weapon4_2.swf":
						Profession_Weapon_Class_list[4] = _class;
						break;
					case PATH_WEAPON+"weapon5_2.swf":
						Profession_Weapon_Class_list[5] = _class;
						break;
				}
				if(_loadNumber >= 14.9)
					loadNPC()
			}
		}
		
		private static function loadNPC():void
		{
			Game.EngineMgr.loadSWF(PATH_BOSS+"boss45.swf",onSwfLoadCompulete);
			Game.EngineMgr.loadSWF(PATH_BOSS+"boss42.swf",onSwfLoadCompulete);
			Game.EngineMgr.loadSWF(PATH_BOSS+"NPC1001.swf",onSwfLoadCompulete);
			Game.EngineMgr.startloading();
			function onSwfLoadCompulete(e:Event,path:String):void
			{
				var info:LoaderInfo = e.currentTarget as LoaderInfo;
				var _class:Class = info.applicationDomain.getDefinition("ViewMain") as Class;
				_loadNumber++;
				switch(path)
				{
					case PATH_BOSS+"boss45.swf":
						NPC_Class_List[1000] = _class;
						break;
					case PATH_BOSS+"boss42.swf":
						NPC_Class_List[1005] = _class;
						break;
					case PATH_BOSS+"NPC1001.swf":
						NPC_Class_List[1001] = _class;
						break;
				}
				if(_loadNumber >= 17.9)
					loadSkill();
			}
		}
		
		private static function loadSkill():void
		{
			Game.EngineMgr.loadSWF(PATH_SIKLL+"11001.swf",onLoadCompulete);
			Game.EngineMgr.loadSWF(PATH_SIKLL+"11002.swf",onLoadCompulete);
			Game.EngineMgr.loadSWF(PATH_SIKLL+"11003.swf",onLoadCompulete);
			Game.EngineMgr.loadSWF(PATH_SIKLL+"11005.swf",onLoadCompulete);
			Game.EngineMgr.loadSWF(PATH_SIKLL+"11006.swf",onLoadCompulete);
			function onLoadCompulete(e:Event,path:String):void
			{
				var info:LoaderInfo = e.currentTarget as LoaderInfo;
				var _class:Class = info.applicationDomain.getDefinition("ViewMain") as Class;
				_loadNumber++;
				switch(path)
				{
					case PATH_SIKLL+"11001.swf":
						Sikll_Class_List[11001] = _class;
						break;
					case PATH_SIKLL+"11002.swf":
						Sikll_Class_List[11002] = _class;
						break;
					case PATH_SIKLL+"11003.swf":
						Sikll_Class_List[11003] = _class;
						break;
					case PATH_SIKLL+"11005.swf":
						Sikll_Class_List[11005] = _class;
						break
					case PATH_SIKLL+"11006.swf":
						Sikll_Class_List[11006] = _class;
						break
				}
				if(_loadNumber >= 22.9)
					loadMap();
			}
		}
		
		
		private static function loadMap():void
		{
			Game.EngineMgr.loadSWF(PATH_MAP+"Map1.swf",onMapCompulete);
			Game.EngineMgr.loadSWF(PATH_MAP+"Map2.swf",onMapCompulete);
			Game.EngineMgr.startloading();
			
			function onMapCompulete(e:Event,path:String):void
			{
				_loadNumber++;
				var info:LoaderInfo = e.currentTarget as LoaderInfo;
				var _mapS:MapSource = new MapSource();
				_mapS.mapSourceClass1 = info.applicationDomain.getDefinition("Fla.PTGame.MapSource1") as Class;
				_mapS.mapSourceClass2 = info.applicationDomain.getDefinition("Fla.PTGame.MapSource2") as Class;
				_mapS.mapSourceClass3 = info.applicationDomain.getDefinition("Fla.PTGame.MapSource3") as Class;
				_mapS.mapSourceClass4 = info.applicationDomain.getDefinition("Fla.PTGame.MapSource4") as Class;
				switch(path)
				{
					case PATH_MAP+"Map1.swf":
						Map_Source_List[1001] = _mapS;
						break;
					case PATH_MAP+"Map2.swf":
						Map_Source_List[1002] = _mapS;
						break;
				}
			}
		}
	}
}